const express = require("express")
const bimi = require('../parser/bimi')
const router = express.Router()

const resp = require('../model/Response')

router.get('/home', (req, res) => {
    bimi.getHome().then(data => {
        res.send(resp.success(data))
    }).catch(e => {
        res.send(resp.failure(JSON.stringify(e)))
    })
})

router.get('/search', (req, res) => {
    console.log('=========== search page ==============')
    console.log(req)
    
    const kw = req.query.kw
    const p = req.query.p
    bimi.search(kw, p).then( data => 
        res.send(resp.success(data))
    ).catch(e => {
        res.send(resp.failure(JSON.stringify(e)))
    })
})

router.get('/detail', (req, res) => {
    const url = req.query.url
    console.log('=========== detai page ==============')
    console.log(req.query)
    
    bimi.detail(url).then(data => {
        res.send(resp.success(data))
    }).catch(e => {
        res.send(resp.failure(JSON.stringify(e)))
    })
})

router.get('/source', (req, res) => {
    const url = req.query.url
    bimi.source(url).then(data => {
        res.send(resp.success(data))
    }).catch(e => {
        res.send(resp.failure(JSON.stringify(e)))
    })
})

router.get("/playUrl", (req, res) => {
    const url = req.query.url
    bimi.playUrl(url).then(data => {
        res.send(resp.success(data))
    }).catch(e => {
        res.send(resp.failure(JSON.stringify(e)))
    })
})

router.get('/related', (req, res) => {
    const url = req.query.url
    bimi.related(url).then(data => {
        res.send(resp.success(data))
    }).catch(e => {
        res.send(resp.failure(JSON.stringify(e)))
    })
})

router.get('/table', (req, res) => {
    const i = req.query.i
    bimi.table(i).then(data => {
        res.send(resp.success(data))
    }).catch(e => {
        res.send(resp.failure(JSON.stringify(e)))
    })
})

router.get('/timeline', (req, res) => {
    bimi.timeline().then(data => {
        res.send(resp.success(data))
    }).catch(e => {
        res.send(resp.failure(JSON.stringify(e)))
    })
})

router.get('/ranking', (req, res) => {
    bimi.ranking().then(data => {
        res.send(resp.success(data))
    }).catch(e => {
        res.send(resp.failure(JSON.stringify(e)))
    })
})

module.exports = router